package com.my.chapter21;

import net.mindview.util.Generator;

import java.util.*;
import java.util.concurrent.*;

public class practice5 {
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        ArrayList<Future<Integer>> results =
                new ArrayList<Future<Integer>>();
        for(int i = 1; i <= 5; i++)
            results.add(exec.submit(new FibonacciSum(i)));
        Thread.yield();
        exec.shutdown();
        for(Future<Integer> fi : results)
            try {
                System.out.println(fi.get());
            } catch(Exception e) {
                e.printStackTrace();
            }
    }
}

class FibonacciSum
        implements Generator<Integer>, Callable<Integer> {
    private int count;
    private final int n;
    public FibonacciSum(int n) { this.n = n; }
    public Integer next() { return fib(count++); }
    private int fib(int n) {
        if(n < 2) return 1;
        return fib(n-2) + fib(n-1);
    }
    public Integer call() {
        int sum = 0;
        for(int i = 0; i < n; i++)
            sum += next();
        return sum;
    }
}
